//
//  ClockAnimationController.m
//  CoreAnimationDemo
//
//  Created by AY on 17/6/1.
//  Copyright © 2017年 Alex. All rights reserved.
//

#import "ClockAnimationController.h"

#define kClockW _clockView.bounds.size.width
#define angle2radion(a) ((a) / 180.0 * M_PI)
#define perSecondA 6
#define perMinuteA 6
#define perHourA 30
#define perMinuteHourA 0.5

@interface ClockAnimationController ()
@property (weak, nonatomic) IBOutlet UIImageView *clockView;
@property (nonatomic, weak) CALayer *secondLayer;
@property (nonatomic, weak) CALayer *minuteLayer;
@property (nonatomic, weak) CALayer *hourLayer;
@end

@implementation ClockAnimationController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self = [self initWithNibName:NSStringFromClass([ClockAnimationController class]) bundle:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpHourLayer];
    [self setUpMinuteLayer];
    [self setUpSecondLayer];

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    [self timeChange];
}

- (void)timeChange {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents  *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
    NSInteger second = cmp.second;
    NSInteger minute = cmp.minute;
    NSInteger hour = cmp.hour;
    CGFloat secondA = second * perSecondA;
    CGFloat minuteA = minute * perMinuteA;
    CGFloat hourA = hour * perHourA + minute * perMinuteHourA;
    _secondLayer.transform = CATransform3DMakeRotation(angle2radion(secondA), 0, 0, 1);
    _minuteLayer.transform = CATransform3DMakeRotation(angle2radion(minuteA), 0, 0, 1);
    _hourLayer.transform = CATransform3DMakeRotation(angle2radion(hourA), 0, 0, 1);
}

#pragma mark - 添加秒针
- (void)setUpSecondLayer {
    CALayer *secondL = [CALayer layer];
    secondL.backgroundColor = [UIColor redColor].CGColor;
    secondL.anchorPoint = CGPointMake(0.5, 1);
    secondL.position = CGPointMake(kClockW * 0.5, kClockW * 0.5);
    secondL.bounds = CGRectMake(0, 0, 1, kClockW * 0.5 - 20);
    [_clockView.layer addSublayer:secondL];
    _secondLayer = secondL;
}

#pragma mark - 添加分针
- (void)setUpMinuteLayer {
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor blackColor].CGColor;
    layer.anchorPoint = CGPointMake(0.5, 1);
    layer.position = CGPointMake(kClockW * 0.5, kClockW * 0.5);
    layer.bounds = CGRectMake(0, 0, 4, kClockW * 0.5 - 20);
    layer.cornerRadius = 4;
    [_clockView.layer addSublayer:layer];
    _minuteLayer = layer;
}

#pragma mark - 添加时针
- (void)setUpHourLayer {
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor blackColor].CGColor;
    layer.anchorPoint = CGPointMake(0.5, 1);
    layer.position = CGPointMake(kClockW * 0.5, kClockW * 0.5);
    layer.bounds = CGRectMake(0, 0, 4, kClockW * 0.5 - 40);
    layer.cornerRadius = 4;
    [_clockView.layer addSublayer:layer];
    _hourLayer = layer;
}

@end
